/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgSelScanCfg.cpp : implementation file

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgSelScanCfg.h"
#include ".\dlgselscancfg.h"

//---------------------------------------------------------------------------

#define SEL_WND_MIN_WIDTH 504
#define SEL_WND_MIN_HEIGHT 280

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgSelScanCfg, CRgnDialog)
DlgSelScanCfg::DlgSelScanCfg(CWnd* pParent /*=NULL*/) : CRgnDialog(DlgSelScanCfg::IDD, pParent)
{
	wcm = NULL;
	skinIn = NULL;

	lastWidth = 0;
	lastHeight = 0;

	retVal = false;
}

DlgSelScanCfg::~DlgSelScanCfg()
{
}

//---------------------------------------------------------------------------

void DlgSelScanCfg::DoDataExchange(CDataExchange* pDX)
{
	CRgnDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LST_SEL_SCAN, scanSelList);
	DDX_Control(pDX, IDC_BTN_SCAN, btnScan);
	DDX_Control(pDX, IDC_BTN_CANCEL, btnCancel);
	DDX_Control(pDX, IDC_BTN_EDIT, btnEdit);
	DDX_Control(pDX, IDC_BTN_NEW, btnNew);
	DDX_Control(pDX, IDC_BTN_EXAMPLE, btnExamples);
	DDX_Control(pDX, IDC_BTN_DEL, btnDel);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgSelScanCfg, CRgnDialog)
	ON_BN_CLICKED(IDC_BTN_CANCEL, OnBnClickedBtnCancel)
	ON_BN_CLICKED(IDC_BTN_SCAN, OnBnClickedBtnScan)
	ON_BN_CLICKED(IDC_BTN_EDIT, OnBnClickedBtnEdit)
	ON_BN_CLICKED(IDC_BTN_NEW, OnBnClickedBtnNew)
	ON_NOTIFY(NM_DBLCLK, IDC_LST_SEL_SCAN, OnNMDblclkLstSelScan)
	ON_BN_CLICKED(IDC_BTN_EXAMPLE, OnBnClickedBtnExample)
	ON_BN_CLICKED(IDC_BTN_DEL, &DlgSelScanCfg::OnBnClickedBtnDel)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

BOOL DlgSelScanCfg::OnInitDialog()
{
	CRgnDialog::OnInitDialog();

	LVCOLUMN col;
	DWORD res;

	scanSelList.SendMessage(LVM_SETVIEW, LV_VIEW_DETAILS, 0);

	res = scanSelList.SetExtendedStyle(LVS_EX_FULLROWSELECT);

	col.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;
	col.fmt = LVCFMT_LEFT;
	col.cx = 150;
	col.pszText = "Description";
	scanSelList.InsertColumn(0, &col);

	col.pszText = "File Name";
	scanSelList.InsertColumn(1, &col);

	col.pszText = "Comment";
	scanSelList.InsertColumn(2, &col);

	LoadValuesFromManager();

	if(lastWidth>=SEL_WND_MIN_WIDTH && lastHeight>=SEL_WND_MIN_HEIGHT){
		// resize as requested
		SetWindowPos(NULL, 0, 0, lastWidth, lastHeight, SWP_NOMOVE);
	}

	SetProperties(skinIn, true, _T("Select"));

	return TRUE;  // return TRUE unless you set the focus to a control
}

//---------------------------------------------------------------------------

void DlgSelScanCfg::ResizeChilds(void)
{
	int xx, yy, ww, hh, bw, bh, dd;
	RECT rect;

	dd = 2;

	btnScan.GetWindowRect(&rect);
	bw = rect.right - rect.left;
	bh = rect.bottom - rect.top;

	xx = clientRect.left;
	yy = clientRect.top;
	ww = clientRect.right - clientRect.left;
	hh = clientRect.bottom - clientRect.top - bh - dd;
	scanSelList.MoveWindow(xx, yy, ww, hh, TRUE);

	xx += ww;
	xx -= bw;
	yy += hh + dd;
	btnCancel.MoveWindow(xx, yy, bw, bh);

	xx -= bw;
	xx -= dd;
	btnScan.MoveWindow(xx, yy, bw, bh);

	xx = clientRect.left;
	btnNew.MoveWindow(xx, yy, bw, bh);

	xx += bw;
	xx += dd;
	btnEdit.MoveWindow(xx, yy, bw, bh);

	xx += bw;
	xx += dd;
	btnExamples.MoveWindow(xx, yy, bw, bh);

	xx += bw;
	xx += dd;
	btnDel.MoveWindow(xx, yy, bw, bh);
}

bool DlgSelScanCfg::GetMinimumSize(RECT* rectIn)
{
	if(rectIn==NULL) return false;

	rectIn->left   = 0;
	rectIn->right  = SEL_WND_MIN_WIDTH;
	rectIn->top    = 0;
	rectIn->bottom = SEL_WND_MIN_HEIGHT;

	return true;
}

//---------------------------------------------------------------------------

void DlgSelScanCfg::LoadValuesFromManager(void)
{
	int i, cnt;

	Initialize();

	if(wcm==NULL) return;

	cnt = wcm->GetSize();
	for(i=0; i<cnt; i++)
		AddElement(wcm->GetByIdx(i));
}

//---------------------------------------------------------------------------

void DlgSelScanCfg::Initialize(void)
{
	scanSelList.DeleteAllItems();
}

bool DlgSelScanCfg::SetItemProperties(int itemIdx, WorkCfg* wcIn)
{
	CString str;

	if(!scanSelList.SetItemData(itemIdx, (DWORD_PTR)wcIn))
		return false;

	str = wcIn->xDescription;
	if(str.IsEmpty()) str = _T("-");
	if(!scanSelList.SetItemText(itemIdx, 0, CT2A(str)))
		return false;

	str = wcIn->xFileName;
	if(str.IsEmpty()) str = _T("-");
	if(!scanSelList.SetItemText(itemIdx, 1, CT2A(str)))
		return false;

	str = wcIn->xComment;
	if(str.IsEmpty()) str = _T("-");
	if(!scanSelList.SetItemText(itemIdx, 2, CT2A(str)))
		return false;

	return true;
}

bool DlgSelScanCfg::AddElement(WorkCfg* wcIn)
{
	LV_ITEM lvi;
	int itemIdx;
	int res;
	CString str;

	if(wcIn==NULL) return true;

	itemIdx = scanSelList.GetItemCount();

	str = wcIn->xDescription;
	if(str.IsEmpty()) str = _T("-");

	lvi.mask     = LVIF_TEXT;
	lvi.iItem    = itemIdx;
	lvi.iSubItem = 0;
	lvi.pszText  = CT2A(str);
	res = scanSelList.InsertItem(&lvi);
	if(res==-1) return false;

	return SetItemProperties(itemIdx, wcIn);
}

//---------------------------------------------------------------------------

void DlgSelScanCfg::OnBnClickedBtnNew()
{
	CString fileName;
	CString fileExt;
	WorkCfg wc;
	bool fileNameOk;

	if(wcm==NULL) return;

	dlgEdit.skinIn = skin;
	dlgEdit.infoString = _T("Enter destination file name:");
	dlgEdit.dataString.Empty();

	fileNameOk = false;
	while(!fileNameOk){
		dlgEdit.SetHelpInfo(helpInfo, HELPID_EDIT_MDSTFILE);
		dlgEdit.DoModal();
		if(!dlgEdit.retVal) return;

		fileName = dlgEdit.dataString;
		fileExt = fileName.Right(4);
		if(fileExt.CompareNoCase(_T(".chs"))!=0){
			// extension must be added
			fileName += _T(".chs");
			dlgEdit.dataString = fileName;
		}
		if(fileOp.IsInvalidCharInFileName(fileName)){
			// invalid file name specification
		}
		else{
			if(fileOp.FileExists(wcm->GetFullConfigFileName(fileName))){
				// file allready there
			}
			else{
				fileNameOk = true;
			}
		}
	}


	dlgScanCfg.wc.Initialize();
	dlgScanCfg.skinIn = skinIn;
	dlgScanCfg.SetHelpInfo(helpInfo, HELPID_DLG_SCANCFG);
	dlgScanCfg.DoModal();
	if(!dlgScanCfg.retVal) return;

	wc.LoadFromPointer(&dlgScanCfg.wc);
	wc.xFileName = fileName;
	wcm->AddConfig(&wc);
	wcm->SaveConfig(&wc);

	LoadValuesFromManager();
}

void DlgSelScanCfg::OnBnClickedBtnEdit()
{
	WorkCfg *wc;
	int itemIdx;

	if(wcm==NULL) return;

	itemIdx = -1;
	itemIdx = scanSelList.GetNextItem(itemIdx, LVNI_SELECTED);
	if(itemIdx==-1) return;

	wc = (WorkCfg *)scanSelList.GetItemData(itemIdx);
	if(wc==NULL) return;

	dlgScanCfg.wc.LoadFromPointer(wc);
	dlgScanCfg.skinIn = skinIn;
	dlgScanCfg.SetHelpInfo(helpInfo, HELPID_DLG_SCANCFG);
	dlgScanCfg.DoModal();
	if(!dlgScanCfg.retVal) return;

	wc->LoadFromPointer(&dlgScanCfg.wc);
	SetItemProperties(itemIdx, wc);
	wcm->SaveConfig(wc);
}

void DlgSelScanCfg::OnBnClickedBtnExample()
{
	if(wcm==NULL) return;

	wcm->SaveExampleConfigurations();
	wcm->Refresh();
	LoadValuesFromManager();
}

void DlgSelScanCfg::OnBnClickedBtnDel()
{
	WorkCfg *wc;
	int itemIdx;

	if(wcm==NULL) return;

	itemIdx = -1;
	itemIdx = scanSelList.GetNextItem(itemIdx, LVNI_SELECTED);
	if(itemIdx==-1) return;

	wc = (WorkCfg *)scanSelList.GetItemData(itemIdx);
	if(wc==NULL) return;

	wcm->DeleteConfig(wc);
	wcm->Refresh();
	LoadValuesFromManager();
}

void DlgSelScanCfg::OnBnClickedBtnScan()
{
	WorkCfg *wc;
	int itemIdx;

	itemIdx = -1;
	itemIdx = scanSelList.GetNextItem(itemIdx, LVNI_SELECTED);
	if(itemIdx==-1) return;

	wc = (WorkCfg *)scanSelList.GetItemData(itemIdx);
	if(wc==NULL) return;

	wcResult.LoadFromPointer(wc);

	MemorizeSize();

	retVal = true;
	EndDialog(IDOK);
}

void DlgSelScanCfg::OnBnClickedBtnCancel()
{
	MemorizeSize();

	retVal = false;
	EndDialog(IDABORT);
}

//---------------------------------------------------------------------------

void DlgSelScanCfg::MemorizeSize(void)
{
	CRect rect;

	GetWindowRect(&rect);
	lastWidth = rect.Width();
	lastHeight = rect.Height();
}

//---------------------------------------------------------------------------

void DlgSelScanCfg::OnOK()
{
	// CRgnDialog::OnOK();
}

void DlgSelScanCfg::OnCancel()
{
	// CRgnDialog::OnCancel();
}

//---------------------------------------------------------------------------

void DlgSelScanCfg::OnNMDblclkLstSelScan(NMHDR *pNMHDR, LRESULT *pResult)
{
	WorkCfg *wc;

	LPNMITEMACTIVATE pNMIA = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	if(pNMIA!=NULL){
		if(pNMIA->iItem>=0){
			wc = (WorkCfg *)scanSelList.GetItemData(pNMIA->iItem);
			if(wc!=NULL){
				wcResult.LoadFromPointer(wc);

				MemorizeSize();

				retVal = true;
				EndDialog(IDOK);
			}
		}
	}

	*pResult = 0;
}

//---------------------------------------------------------------------------
